package com.example.electronicmall.application;

import android.content.Context;
import android.util.Log;

import com.example.electronicmall.bean.CommdityData;
import com.example.electronicmall.bean.CommdityOneBean;
import com.example.electronicmall.bean.CommentBean;
import com.example.electronicmall.bean.ProductOne;
import com.example.electronicmall.http.IRequestShopp;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by
 * Time: 18:48
 * Author: Lenovo
 */
public class CommodityPresenter implements IRequestShopp{

    private static final String TAG = CommodityPresenter.class.getSimpleName();

    private Context context;

    public CommodityPresenter(Context context) {
        this.context = context;
    }

    //二级
    @Override
    public void requestCommodity(OnCommodityResultListener listener) {
        BaseApplication.request.requestComment()
                .enqueue(new Callback<CommdityOneBean<CommdityData>>() {
                    @Override
                    public void onResponse(Call<CommdityOneBean<CommdityData>> call, Response<CommdityOneBean<CommdityData>> response) {
                        CommdityData[] commdityData=response.body().getData();
                        for (int i = 0; i < commdityData.length; i++) {
                            CommentBean[] commentBean=commdityData[i].getChildren();
                            listener.onCommentBeanResult(commentBean);
                        }

                    }

                    @Override
                    public void onFailure(Call<CommdityOneBean<CommdityData>> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
    }

    @Override
    public void requestLeftCommodity(OnLeftResultListener listener) {
        BaseApplication.request.requestLeftComment()
                .enqueue(new Callback<CommdityOneBean>() {
                    @Override
                    public void onResponse(Call<CommdityOneBean> call, Response<CommdityOneBean> response) {
                        CommdityData[] commdityData=response.body().getData();
                        listener.onLeftBeanResult(commdityData);
                    }

                    @Override
                    public void onFailure(Call<CommdityOneBean> call, Throwable t) {

                    }
                });
    }

    @Override
    public void getProductList(OnProductListener listener) {
        BaseApplication.request.getProduct()
                .enqueue(new Callback<ProductOne>() {
                    @Override
                    public void onResponse(Call<ProductOne> call, Response<ProductOne> response) {
                        ProductOne.ProductData.ProductBean[] productOne=response.body().getData().getProductList();
                        listener.onGetProductList(productOne);
                        Log.d(TAG, "onResponse: "+productOne);
                    }

                    @Override
                    public void onFailure(Call<ProductOne> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
    }




  /*  @Override
    public void requestCommodity(OnCommodityResultListener listener) {
        BaseApplication.request.requestComment()
                .enqueue(new Callback<CommdityOneBean<CommdityData>>() {
                    @Override
                    public void onResponse(Call<CommdityOneBean<CommdityData>> call, Response<CommdityOneBean<CommdityData>> response) {
                        CommentBean[] commdityData=response.body().getData().getPmsProductCategoryList();
                        Log.d(TAG, "onResponse: "+commdityData);
                        listener.onCommentBeanResult(commdityData);
                    }

                    @Override
                    public void onFailure(Call<CommdityOneBean<CommdityData>> call, Throwable t) {

                    }
                });*/

    //商品分类一级
    public interface OnCommodityResultListener {
        void onCommentBeanResult(CommentBean[] commentBeans);
    }
    //商品分类一级
    public interface OnLeftResultListener {
        void onLeftBeanResult(CommdityData[] commdityData);
    }
    //商品分类一级
    public interface OnProductListener {
        void onGetProductList(ProductOne.ProductData.ProductBean[] productBean);
    }
}
